﻿using System;

namespace YArchitech.Plumbing
{
	public class HYPlumbBaseCal
	{
		public static bool HZWLYL_Get_VAndI(double Q, double D, double y, double C, ref double V, ref double I)
		{
			if (!HYPlumbBaseCal.Get_V(Q, D, ref V))
			{
				return false;
			}
			double re = 0.0;
			return HYPlumbBaseCal.Get_Re(y, V, D, ref re) && HYPlumbBaseCal.Get_I(V, D, re, C, ref I);
		}

		public static bool MLYL_Get_VAndI(double Q, double D, double n, ref double V, ref double I)
		{
			D /= 1000.0;
			return HYPlumbBaseCal.Get_V(Q, D, ref V) && HYPlumbBaseCal.Get_I(V, D, n, ref I);
		}

		public static bool Get_V(double Q, double D, ref double V)
		{
			if (Math.Abs(D) < 1E-10)
			{
				HYPlumbBaseCal.szError = "数学函数异常,除数不能为零!";
				return false;
			}
			V = 4.0 * Q / (Math.PI * D * D);
			return true;
		}

		public static bool GetY(double C, ref double Y)
		{
			Y = 0.01775 / (1.0 + 0.0337 * C + 0.000221 * C * C);
			return true;
		}

		public static bool Get_Re(double y, double V, double D, ref double Re)
		{
			if (Math.Abs(y) < 1E-10)
			{
				HYPlumbBaseCal.szError = "水的运动粘度(y)为零！";
				return false;
			}
			Re = V * D / y;
			return true;
		}

		public static bool Get_I(double V, double D, double Re, double C, ref double I)
		{
			if (Math.Abs(C) < 1E-10)
			{
				HYPlumbBaseCal.szError = "C为零!";
				return false;
			}
			double num = Re / C;
			if (num < 0.0)
			{
				HYPlumbBaseCal.szError = "数据函数出现异常!";
				return false;
			}
			num = 0.044 * C * Math.Pow(num, 0.075);
			if (Math.Abs(num) < 1E-10 || Math.Abs(9.81 * D) < 1E-10)
			{
				HYPlumbBaseCal.szError = "被零除!";
				return false;
			}
			num = V / num;
			I = num * num / (9.81 * D);
			return true;
		}

		public static bool Get_I(double V, double D, double n, ref double I)
		{
			if (D <= 0.0)
			{
				HYPlumbBaseCal.szError = "数据错误!";
				return false;
			}
			double num = V * n / Math.Pow(D / 4.0, 0.66666666666666663);
			I = num * num;
			return true;
		}

		public static string szError;
	}
}
